*makes Figures 1, 4, 7, 8, 9, 10

set more off
clear
capture log close

* SET DIRECTORY HERE 
cd "C:\Users\mitch\Dropbox\BLW\POP\Searching for a Bright Line\Searching for a Bright Line replication materials" 

log using "Logs/BLW-figures 1,4,7,8,9,10.log", replace text

* Extract relevant data from V-Dem
* The data originate in V-Dem dataset Version 8, available from https://www.v-dem.net/en/data/data-version-8/
* The particular file used is included in the archive Country_Year_V-Dem_Extended_STATA_v8
* 
* use "Data/V-Dem-CY+Others-v8.dta", clear
* keep if country_text_id=="USA"
* keep year v2x_libde*
* gen VDEM_US = v2x_libdem * 100
* gen VDEM_US_ll = (v2x_libdem-(1.96*v2x_libdem_sd)) * 100
* gen VDEM_US_ul = (v2x_libdem+(1.96*v2x_libdem_sd)) * 100
* keep year VDEM_U*
* list
* save "Data/V-Dem-CY-Core-v8-for-merge.dta", replace

*open BLW experts data

use "Data/BLW_Expert_Waves123456.dta", clear

egen USrating_w3_all=mean(USrating) if wave==3
drop US_2017
gen US_2017=USrating_w3 if wave==3 /*replace with final expert survey of 2017*/
gen US_2018=USrating if wave==6 /*replace with most recent expert survey of 2018*/

foreach var of varlist US* {
gen SE`var'=`var'
}

collapse (mean) US* (semean) SEUS* 
gen id=_n
reshape long US_, i(id) j(year)
rename US_ BLW_US

sort year
merge 1:1 year using "Data/V-Dem-CY-Core-v8-for-merge.dta"
tab _merge
drop _merge
sort year

gen BLW_US_ul=.
replace BLW_US_ul=BLW_US+(1.96*SEUS_1800) if year==1800
replace BLW_US_ul=BLW_US+(1.96*SEUS_1850) if year==1850
replace BLW_US_ul=BLW_US+(1.96*SEUS_1900) if year==1900
replace BLW_US_ul=BLW_US+(1.96*SEUS_1950) if year==1950
replace BLW_US_ul=BLW_US+(1.96*SEUS_1975) if year==1975
replace BLW_US_ul=BLW_US+(1.96*SEUS_1985) if year==1985
replace BLW_US_ul=BLW_US+(1.96*SEUS_1995) if year==1995
replace BLW_US_ul=BLW_US+(1.96*SEUS_2005) if year==2005
replace BLW_US_ul=BLW_US+(1.96*SEUS_2015) if year==2015
replace BLW_US_ul=BLW_US+(1.96*SEUS_2017) if year==2017
replace BLW_US_ul=BLW_US+(1.96*SEUS_2018) if year==2018

gen BLW_US_ll=.
replace BLW_US_ll=BLW_US-(1.96*SEUS_1800) if year==1800
replace BLW_US_ll=BLW_US-(1.96*SEUS_1850) if year==1850
replace BLW_US_ll=BLW_US-(1.96*SEUS_1900) if year==1900
replace BLW_US_ll=BLW_US-(1.96*SEUS_1950) if year==1950
replace BLW_US_ll=BLW_US-(1.96*SEUS_1975) if year==1975
replace BLW_US_ll=BLW_US-(1.96*SEUS_1985) if year==1985
replace BLW_US_ll=BLW_US-(1.96*SEUS_1995) if year==1995
replace BLW_US_ll=BLW_US-(1.96*SEUS_2005) if year==2005
replace BLW_US_ll=BLW_US-(1.96*SEUS_2015) if year==2015
replace BLW_US_ll=BLW_US-(1.96*SEUS_2017) if year==2017
replace BLW_US_ll=BLW_US-(1.96*SEUS_2018) if year==2018

gen fake1=.
gen fake2=.

*Figure 1
twoway (rspike VDEM_US_ul VDEM_US_ll year, graphregion(color(white)) bgcolor(white) ///
fcolor(white) color(white) lcolor(gs12) lpattern(solid)) (connected VDEM_US year, lpattern(solid) lcolor(gs8) mcolor(gs8) msymbol(o) ///
msize(*.25)) (connected BLW_US year, msize(*.25) lpattern(dash) scheme(plotplain) legend(order(5 6) lab(5 "V-Dem") lab(6 "BLW")) ylabel(0(20)100) ///
xmtick(1775(50)2025) xtitle("") msymbol(d) mcolor(black) title("U.S. democratic performance") subtitle("Expert ratings: 1789-2018",size(*.8))) ///
(rspike BLW_US_ul BLW_US_ll year,lcolor(black)) (connected fake1 year, lcolor(gs8) mcolor(gs8) msymbol(o) msize(*.25)  lpattern(solid))  ///
(connected fake2 year, lcolor(black) msize(*.25) mcolor(black) msymbol(d) lpattern(dash)) 

graph export "Figures/fig1.png", width(2000) replace
* graph export "Figures/fig1.tif", width(10000) replace

* rm "Data/V-Dem-CY-Core-v8-for-merge.dta"



use "Data/BLW_Expert_Waves123456.dta", clear

gen id=_n
reshape long perf_, i(id) j(statement) string

*substitute in clean labels
drop if statement=="restraint" | statement=="democracy_today" | statement=="science"
replace statement="All parties allowed" if statement=="ban_ideology"
replace statement="Free speech" if statement=="free_speech"
replace statement="Fraud-free elections" if statement=="election_fraud"
replace statement="Protest tolerated" if statement=="protest"
replace statement="Judiciary can limit executive" if statement=="judiciary_check"
replace statement="Opinions heard on policy" if statement=="opinions"
replace statement="Judicial independence" if statement=="judicial_ind" | statement=="judicial_independence"
replace statement="No interference with press" if statement=="journalists"
replace statement="Agencies do not punish" if statement=="monitor_opponents"
replace statement="No political violence" if statement=="private_violence"
replace statement="Legislature can limit executive" if statement=="legislature_check"
replace statement="Investigations not compromised" if statement=="investigations"
replace statement="Sanctions for misconduct" if statement=="misconduct"
replace statement="Equal political/legal rights" if statement=="equal_rights"
replace statement="Equal voting rights" if statement=="voting_rights"
replace statement="No foreign influence" if statement=="foreign_influence"
replace statement="No private gains from office" if statement=="private_gain"
replace statement="Campaign funds transparent" if statement=="funds_transparent"
replace statement="Votes have equal impact" if statement=="votes_impact"
replace statement="Patriotism not questioned" if statement=="patriotism"
replace statement="Common understanding of facts" if statement=="common_und" | statement=="common_understanding"
replace statement="Contribs not determine policy" if statement=="contributions" | statement=="contributions_determine"
replace statement="Participation high" if statement=="participation"
replace statement="Compromise sought" if statement=="compromise"
replace statement="Districts not biased" if statement=="districts"
replace statement="Constitution limits executive" if statement=="expand_executive"
replace statement="Candidates disclose info" if statement=="candidates_disclose" 

*create binary measure
gen binaryperf=.
replace binaryperf=0 if (perf_==1 | perf_==2)
replace binaryperf=1 if (perf_==3 | perf_==4)

su binaryperf if statement=="Opinions heard on policy" & wave==6
su binaryperf if statement=="Fraud-free elections" & wave==6
su binaryperf if statement=="Legislature can limit executive" & wave==6
su binaryperf if statement=="Sanctions for misconduct" & wave==6

preserve
gen altbinaryperf=0
replace altbinaryperf=1 if (perf_==3 | perf_==4)

collapse (mean) binaryperf altbinaryperf,by(statement) 
gsort - altbinaryperf
list
restore

*create ordering variable
bysort wave statement: egen meanbinaryperf=mean(binaryperf) if wave==3
bysort statement: egen meanbinaryperf2=mean(meanbinaryperf)

bysort wave statement: egen meanbinaryperf6=mean(binaryperf) if wave==6
bysort statement: egen meanbinaryperf62=mean(meanbinaryperf6)

separate binaryperf,by(wave)

preserve
gen wt=1
drop expert
gen expert=1
rename statement statement2
keep wave expert wt binaryperf statement2 
save "Data/expert-perf.dta", replace
restore

preserve

gen point=.
gen ll=.
gen ul=.
egen statementnum=group(statement)
forval i=1/27 {
reg binaryperf i.wave if (wave==3 | wave==6) & statementnum==`i'
matrix A=r(table)
replace point=A[1,2] if statementnum==`i'
replace ll=A[5,2] if statementnum==`i'
replace ul=A[6,2] if statementnum==`i'
}

collapse (mean) point ll ul, by(statement)

gsort +point
gen ordernum=_n
export delimited statement using "Figures/fig9ylabels.csv", replace /*use this to generate label order*/
 
*Figure 9
twoway (scatter ordernum point, xlabel(-.3 "-30%" -.2 "-20%" -.1 "-10%"0 "0%" .1 "+10%",labsize(*.8)) msymbol(O) mcolor(green)) ///
(rspike ll ul ordernum, horizontal lcolor(green) scheme(s2color) legend(off) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) xtitle(`"Change in percentage answering U.S. "fully" or "mostly meets" standard"', ///
size(*.7)) ylabel(27 "Candidates disclose info" 26 "Compromise sought" 25 "Campaign funds transparent" 24 "Contribs not determine policy" ///
23 "Districts not biased" 22 "Protest tolerated" 21 "Participation high" 20 "All parties allowed" 19 "Opinions heard on policy" 18 "Votes have equal impact" ///
17 "No political violence" 16 "Common understanding of facts" 15 "Free speech" 14 "Agencies do not punish" 13 "Investigations not compromised" ///
12 "No interference with press" 11 "Fraud-free elections" 10 "Patriotism not questioned" 9 "Equal voting rights" 8 "No foreign influence" ///
7 "No private gains from office" 6 "Equal political/legal rights" 5 "Sanctions for misconduct" 4 "Constitution limits executive" 3 "Judicial independence" ///
2 "Legislature can limit executive" 1 "Judiciary can limit executive", angle(0) labsize(*.75)) ytitle("") xline(0,lcolor(gs10)))

graph export "Figures/fig9.png", width(2000) replace
* graph export "Figures/fig9.tif", width(10000) replace

restore




egen statementnum2 = group(statement)
gen statementmeanW6experts=.
gen expertll=.
gen expertul=.
forvalues i = 1/27 { 
mean binaryperf if statementnum2 == `i' & wave == 6 
matrix A=r(table)
local blah=A[1,1]
replace statementmeanW6experts = `blah' if statementnum2 == `i' 
local blah2=A[5,1]
replace expertll = `blah2' if statementnum2 == `i' 
local blah3=A[6,1]
replace expertul = `blah3' if statementnum2 == `i' 
} 

gen statementmeanW2experts=.
gen expertw2ll=.
gen expertw2ul=.
forvalues i = 1/27 { 
mean binaryperf if statementnum2 == `i' & wave == 2 
matrix A=r(table)
local blah=A[1,1]
replace statementmeanW2experts = `blah' if statementnum2 == `i' 
local blah2=A[5,1]
replace expertw2ll = `blah2' if statementnum2 == `i' 
local blah3=A[6,1]
replace expertw2ul = `blah3' if statementnum2 == `i' 
} 

gen statementmeanW1experts=.
gen expertw1ll=.
gen expertw1ul=.
egen wave1num=group(statementnum2) if wave==1 & binaryperf!=.
forvalues i = 1/17 { 
mean binaryperf if wave1num == `i' & wave == 1 
matrix A=r(table)
local blah=A[1,1]
replace statementmeanW1experts = `blah' if wave1num == `i' 
local blah2=A[5,1]
replace expertw1ll = `blah2' if wave1num == `i' 
local blah3=A[6,1]
replace expertw1ul = `blah3' if wave1num == `i' 
} 

preserve

collapse (mean) expertw1ll expertw2ll expertll expertw1ul expertw2ul expertul statementmeanW1experts statementmeanW2experts statementmeanW6experts, by(statement)

gsort +statementmeanW6experts
gen ordernum=_n
export delimited statement using "Figures/fig8ylabels.csv", replace 
 
gen expertw1fake=.
gen expertw2fake=.
gen expertw6fake=.
 
*Figure 8
twoway (scatter ordernum statementmeanW1experts if statementmeanW1experts!=., ///
xlabel(0 "0%" .2 "20%" .4 "40%" .6 "60%" .8 "80%" 1 "100%",labsize(*.8)) msymbol(Oh) mcolor(green*0.5)) ///
(rspike expertw1ll expertw1ul ordernum if statementmeanW1experts!=., horizontal lcolor(green*0.5) scheme(s2color) ///
legend(row(1) pos(6) order(7 8 9) size(*.675) label(7 "Wave 1 (Feb. 2017)") label(8 "Wave 2 (May 2017)") ///
label(9 "Wave 6 (July 2018)") symxsize(*.475)) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
xtitle(`"Percentage who answered U.S. "fully" or "mostly meets" standard"', size(*.7))) ///
(scatter ordernum statementmeanW2experts if statementmeanW1experts==., msymbol(Th) mcolor(green*0.7)) ///
(rspike expertw2ll expertw2ul ordernum if statementmeanW1experts==., horizontal lcolor(green*0.7)) ///
(scatter ordernum statementmeanW6experts, msymbol(Sh) mcolor(green)) (rspike expertll expertul ordernum, horizontal lcolor(green)) ///
(connected ordernum expertw1fake, xscale(r(0 1)) msymbol(Oh) mcolor(green*0.5) lcolor(green*0.5)) ///
(connected ordernum expertw2fake, xscale(r(0 1)) msymbol(Th) mcolor(green*0.7) lcolor(green*0.7) ///
ylabel(27 "All parties allowed" 26 "Protest tolerated" 25 "Free speech" 24 "Opinions heard on policy" 23 "Fraud-free elections" ///
22 "Candidates disclose info" 21 "No political violence" 20 "Agencies do not punish" 19 "No interference with press" 18 "Judiciary can limit executive" ///
17 "Judicial independence" 16 "Investigations not compromised" 15 "Equal political/legal rights" 14 "Equal voting rights" ///
13 "Legislature can limit executive" 12 "Sanctions for misconduct" 11 "Constitution limits executive" 10 "Campaign funds transparent" ///
9 "No foreign influence" 8 "Votes have equal impact" 7 "No private gains from office" 6 "Contribs not determine policy" 5 "Patriotism not questioned" ///
4 "Participation high" 3 "Compromise sought" 2 "Common understanding of facts" 1 "Districts not biased", ///
angle(0) labsize(*.75)) ytitle(""))(connected ordernum expertw6fake, msymbol(Sh) mcolor(green) lcolor(green) ytitle(""))

graph export "Figures/fig8.png", width(2000) replace
* graph export "Figures/fig8.tif", width(10000) replace

restore







*Public

set more off
use "Data/BLW_Public_Waves3456.dta", clear
gen expert=0

*Reshape to create the single variable perf_ indicating US performance for each statement
reshape long perf_, i(caseid) j(statement) string

*Rename again to create labels
gen statement2 = "All parties allowed" if statement == "ban_ideology" 
replace statement2 = "Free speech" if statement == "free_speech"
replace statement2 = "Fraud-free elections" if statement == "election_fraud"
replace statement2 = "Protest tolerated" if statement == "protest"
replace statement2 = "Judiciary can limit executive" if statement == "judiciary_check"
replace statement2 = "Opinions heard on policy" if statement == "opinions"
replace statement2 = "Judicial independence" if statement == "judicial_ind"
replace statement2 = "No interference with press" if statement == "journalists"
replace statement2 = "Agencies do not punish" if statement == "monitor_opponents"
replace statement2 = "No political violence" if statement == "private_violence"
replace statement2 = "Candidates disclose info" if statement == "candidates_disclose"
replace statement2 = "Legislature can limit executive" if statement == "legislature_check"
replace statement2 = "Constitution limits executive" if statement == "expand_executive"
replace statement2 = "Investigations not compromised" if statement == "investigations"
replace statement2 = "Sanctions for misconduct" if statement == "misconduct"
replace statement2 = "Equal political/legal rights" if statement == "equal_rights"
replace statement2 = "Equal voting rights" if statement == "voting_rights"
replace statement2 = "No foreign influence" if statement == "foreign"
replace statement2 = "No private gains from office" if statement == "private_gain"
replace statement2 = "Campaign funds transparent" if statement == "funds_transparent"
replace statement2 = "Votes have equal impact" if statement == "votes_impact"
replace statement2 = "Patriotism not questioned" if statement == "patriotism"
replace statement2 = "Common understanding of facts" if statement == "common_und"
replace statement2 = "Contribs not determine policy" if statement == "contributions"
replace statement2 = "Participation high" if statement == "participation"
replace statement2 = "Compromise sought" if statement == "compromise"
replace statement2 = "Districts not biased" if statement == "districts"

*Create binary measure
gen binaryperf=.
replace binaryperf=0 if (perf_ == 1 | perf_ == 2)
replace binaryperf=1 if (perf_ == 3 | perf_ == 4)

*create ordering variable
egen statementnum = group(statement)
svyset [pweight=weight]

gen statementmean=.
forvalues i = 1/27 { 
svy: mean binaryperf if statementnum == `i' & wave == 6
matrix A=e(b)
local blah=A[1,1]
replace statementmean = `blah' if statementnum == `i' 
} 

gen statementmean2=.
forvalues i = 1/27 { 
svy: mean binaryperf if statementnum == `i' & wave == 3
matrix A=e(b)
local blah=A[1,1]
replace statementmean2 = `blah' if statementnum == `i' 
} 

gen statementmean3=.
forvalues i = 1/27 { 
svy: mean binaryperf if statementnum == `i' & wave == 5
matrix A=e(b)
local blah=A[1,1]
replace statementmean3 = `blah' if statementnum == `i' 
} 

*Separate variables (and rename new ones)
separate binaryperf, by(wave)

gen trump = approve_trmp
recode trump (2 = 1) (3 = 0) (4 = 0) (8 = .) (9 = .)
label define trumpLabel 0 "Not approve Trump" 1 "Approve Trump"
label value trump trumpLabel

separate binaryperf, by(trump)
rename binaryperf0 rating_notapprove
rename binaryperf1 rating_approve

gen statementmeanW6Trump1=.
forvalues i = 1/27 { 
svy: mean binaryperf if statementnum == `i' & wave == 6 & trump == 1
matrix A=e(b)
local blah=A[1,1]
replace statementmeanW6Trump1 = `blah' if statementnum == `i' 
} 

preserve

gen point=.
gen ll=.
gen ul=.
forval i=1/27 {
svy: reg binaryperf i.wave if (wave==3 | wave==6) & statementnum==`i'
matrix A=r(table)
replace point=A[1,2] if statementnum==`i'
replace ll=A[5,2] if statementnum==`i'
replace ul=A[6,2] if statementnum==`i'
}

collapse (mean) point ll ul, by(statement2)

gsort +point
gen ordernum=_n
export delimited statement2 using "Figures/fig10ylabels.csv", replace /*use this to generate label order*/

*Figure 10
twoway (scatter ordernum point, ///
xlabel(-.3 "-30%" -.2 "-20%" -.1 "-10%"0 "0%" .1 "+10%",labsize(*.8)) msymbol(s) mcolor(purple)) ///
(rspike ll ul ordernum, horizontal lcolor(purple) scheme(s2color) legend(off) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
xtitle(`"Change in percentage answering U.S. "fully" or "mostly meets" standard"', size(*.7)) ///
ylabel(27 "Contribs not determine policy " 26 "Participation high " 25 "Equal voting rights " 24 "Campaign funds transparent " 23 "Protest tolerated " ///
22 "Candidates disclose info " 21 "Legislature can limit executive " 20 "Patriotism not questioned " 19 "Investigations not compromised " ///
18 "Districts not biased " 17 "No political violence " 16 "Opinions heard on policy " 15 "No private gains from office " 14 "Sanctions for misconduct " ///
13 "Constitution limits executive " 12 "No foreign influence " 11 "Compromise sought " 10 "Votes have equal impact " 9 "Fraud-free elections " ///
8 "Agencies do not punish " 7 "Equal political/legal rights " 6 "All parties allowed " 5 "Common understanding of facts " 4 "Judiciary can limit executive " ///
3 "Free speech " 2 "Judicial independence " 1 "No interference with press", angle(0) labsize(*.75)) ytitle("") xline(0,lcolor(gs10)))

graph export "Figures/fig10.png", width(2000) replace
* graph export "Figures/fig10.tif", width(10000) replace


restore

append using "Data/expert-perf.dta" 
replace expert=0 if expert==.
rm "Data/expert-perf.dta" 

gen binaryperf0=binaryperf if expert==1 & wave==6

capture drop statementnum2
egen statementnum2 = group(statement2)

gen statementmeanW6public=.
gen publicll=.
gen publicul=.
forvalues i = 1/27 { 
svy: mean binaryperf if statementnum2 == `i' & wave == 6 & expert==0
matrix A=r(table)
local blah=A[1,1]
replace statementmeanW6public = `blah' if statementnum2 == `i' 
local blah2=A[5,1]
replace publicll = `blah2' if statementnum2 == `i' 
local blah3=A[6,1]
replace publicul = `blah3' if statementnum2 == `i' 
} 

gen statementmeanW6experts=.
gen expertll=.
gen expertul=.
forvalues i = 1/27 { 
mean binaryperf if statementnum2 == `i' & wave == 6 & expert==1
matrix A=r(table)
local blah=A[1,1]
replace statementmeanW6experts = `blah' if statementnum2 == `i' & expert==1
local blah2=A[5,1]
replace expertll = `blah2' if statementnum2 == `i' & expert==1
local blah3=A[6,1]
replace expertul = `blah3' if statementnum2 == `i' & expert==1
} 

preserve

collapse (mean) publicll publicul statementmeanW6public expertll expertul statementmeanW6experts, by(statement2)

gsort +statementmeanW6experts
gen ordernum=_n
export delimited statement2 using "Figures/fig4ylabels.csv", replace /*use this to generate label order*/
 
gen publicfake=.
gen expertfake=.

*Figure 4
twoway (scatter ordernum statementmeanW6public,ylabel(1(1)27,grid) xlabel(0 "0%" .2 "20%" .4 "40%" .6 "60%" .8 "80%" 1 "100%",labsize(*.8)) ///
msymbol(d) mcolor(purple)) (rspike publicll publicul ordernum, horizontal lcolor(purple) scheme(s2color) legend(row(1) pos(6) order(5 6) size(*.7) ///
label(5 "Public") label(6 "Experts") symxsize(*.5)) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) xtitle(`"Percentage who answered U.S. "fully" or "mostly meets" standard"', size(*.7))) ///
(scatter ordernum statementmeanW6experts, msymbol(Oh) mcolor(green)) (rspike expertll expertul ordernum, horizontal lcolor(green)) ///
(connected ordernum publicfake, xscale(r(0 1)) msymbol(d) mcolor(purple) lcolor(purple)) (connected ordernum expertfake, xscale(r(0 1)) ///
msymbol(Oh) mcolor(green) lcolor(green) ///
ylabel(27 "All parties allowed" 26 "Protest tolerated" 25 "Free speech" 24 "Opinions heard on policy" 23 "Fraud-free elections" ///
22 "Candidates disclose info" 21 "No political violence" 20 "Agencies do not punish" 19 "No interference with press" 18 "Judiciary can limit executive" ///
17 "Judicial independence" 16 "Investigations not compromised" 15 "Equal political/legal rights" 14 "Equal voting rights" ///
13 "Legislature can limit executive" 12 "Sanctions for misconduct" 11 "Constitution limits executive" 10 "Campaign funds transparent" ///
9 "No foreign influence" 8 "Votes have equal impact" 7 "No private gains from office" 6 "Contribs not determine policy" 5 "Patriotism not questioned" ///
4 "Participation high" 3 "Compromise sought" 2 "Common understanding of facts" 1 "Districts not biased", ///
angle(0) labsize(*.75)) ytitle(""))

graph export "Figures/fig4.png", width(2000) replace
* graph export "Figures/fig4.tif", width(10000) replace

restore

gen statementmeanW6disapprove=.
gen disapprovell=.
gen disapproveul=.
forvalues i = 1/27 { 
svy: mean binaryperf if statementnum2 == `i' & wave == 6 & trump==0
matrix A=r(table)
local blah=A[1,1]
replace statementmeanW6disapprove = `blah' if statementnum2 == `i' 
local blah2=A[5,1]
replace disapprovell = `blah2' if statementnum2 == `i' 
local blah3=A[6,1]
replace disapproveul = `blah3' if statementnum2 == `i' 
} 

gen statementmeanW6approve=.
gen approvell=.
gen approveul=.
forvalues i = 1/27 { 
svy: mean binaryperf if statementnum2 == `i' & wave == 6 & trump==1
matrix A=r(table)
local blah=A[1,1]
replace statementmeanW6approve = `blah' if statementnum2 == `i' 
local blah2=A[5,1]
replace approvell = `blah2' if statementnum2 == `i' 
local blah3=A[6,1]
replace approveul = `blah3' if statementnum2 == `i' 
} 

preserve

collapse (mean) disapprovell disapproveul statementmeanW6disapprove approvell approveul statementmeanW6approve statementmeanW6public, by(statement2)

gsort +statementmeanW6public
gen ordernum=_n
export delimited statement2 using "Figures/fig7ylabels.csv", replace /*use this to generate label order*/
 
gen disapprovefake=.
gen approvefake=.
 
*Figure 7
twoway (scatter ordernum statementmeanW6disapprove,ylabel(1(1)27,grid) xlabel(0 "0%" .2 "20%" .4 "40%" .6 "60%" .8 "80%" 1 "100%",labsize(*.8)) ///
msymbol(T) mcolor(blue)) (rspike disapprovell disapproveul ordernum, horizontal lcolor(blue) scheme(s2color) legend(row(1) pos(6) order(5 6) size(*.7) ///
label(5 "Disapprove of Trump") label(6 "Approve of Trump") symxsize(*.5)) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) xtitle(`"Percentage who answered U.S. "fully" or "mostly meets" standard"', ///
size(*.7))) (scatter ordernum statementmeanW6approve, msymbol(Sh) mcolor(red)) (rspike approvell approveul ordernum, horizontal lcolor(red)) ///
(connected ordernum disapprovefake, xscale(r(0 1)) msymbol(T) mcolor(blue) lcolor(blue)) (connected ordernum approvefake, ///
xscale(r(0 1)) msymbol(Sh) mcolor(red) lcolor(red) ///
ylabel(27 "Equal voting rights" 26 "Protest tolerated" 25 "Opinions heard on policy" 24 "All parties allowed" 23 "Free speech" 22 "Candidates disclose info" ///
21 "Constitution limits executive" 20 "Judiciary can limit executive" 19 "Fraud-free elections" 18 "Legislature can limit executive" ///
17 "Votes have equal impact" 16 "Judicial independence" 15 "Equal political/legal rights" 14 "No interference with press" 13 "No foreign influence" ///
12 "Participation high" 11 "Campaign funds transparent" 10 "Agencies do not punish" 9 "Districts not biased" 8 "No political violence" ///
7 "Investigations not compromised" 6 "Sanctions for misconduct" 5 "Contribs not determine policy" 4 "Compromise sought" 3 "Common understanding of facts" ///
2 "No private gains from office" 1 "Patriotism not questioned", angle(0) labsize(*.75)) ytitle(""))

graph export "Figures/fig7.png", width(2000) replace
* graph export "Figures/fig7.tif", width(10000) replace

restore

log close
